package models;

import play.*;
import play.db.jpa.*;

import javax.persistence.*;
import java.util.*;
import controllers.CRUD.Hidden;

@Entity
public class Level extends Model {
	
	@ManyToOne
	public Developer applicationId;
	@Hidden
	public boolean current;
	
	
	@ManyToOne
	public Point point;
	
	public String title;
	
	public String description;

	
	public long trashhold;
	
	
	@Override
  public String toString() {
    return this.title;
  }

	@PrePersist
	protected void onCreate(){
		if( point.defaultValue >= trashhold){
			current = true;
		}
	}

  public boolean execute(){
	    

		if(point.defaultValue >= trashhold && current == false){
			
			
			List<Level> l = Level.find("byCurrentAndApplicationIdAndPoint", true, applicationId, point).fetch();
			for(Level level : l) {
				level.current = false;
				level.save();
			}
			
			
			current = true;
			return true;
		}else if(point.defaultValue >= trashhold && current == true){
			
			return false;
			
		}
		 
	 	current = false;
		return false;
		
  }
  
	
}

